package com.baichang.taoshunda.controller.mobile.delivery;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(ControllerConstants.ORDER)
public class DeliveryOrderMController extends BaseMobileController {


    @Autowired
    private OrderService orderService;

    //配送员获取订单列表
    @RequestMapping("/getOrderListByOrderState")
    public void getOrderListByOrderState(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                int orderState = getInt(json, "orderState", 3);
                int disId = getInt(json, "disId", 0);
                int nowPage = getInt(json, "nowPage", 1);
                int pageSize = getInt(json, "pageSize", 20);
                Double dlng = getDouble(json, "dlng", 117.14917829518997000000);
                Double dlat = getDouble(json, "dlat", 36.66692040377557000000);

                return orderService.getOrderListByOrderState(orderState, dlng, dlat,disId,nowPage,pageSize);
            }
        });
    }

    //配送员抢单
    @RequestMapping("/grabOrder")
    public void grabOrder(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                int disId = getInt(json, "disId", 0);
                int orderId = getInt(json, "orderId", 0);

                return orderService.grabOrder(disId, orderId);
            }
        });
    }

//    //获取订单详情
//    @RequestMapping("/getOrderByOrderNumber")
//    public void getOrderByOrderNumber(HttpServletRequest request, HttpServletResponse response) {
//        initEncode(request, response, new IService() {
//            @Override
//            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
//
//                String orderNumber = getString(json, "orderNumber", "");
//                Double dlng = getDouble(json, "dlng", 0.0);
//                Double dlat = getDouble(json, "dlat", 0.0);
//
//                return orderService.getOrderByOrderNumber(orderNumber,dlng,dlat);
//            }
//        });
//    }

    /**
     * 查看订单详情
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getDeliveryOrderDetail")
    public void getDeliveryOrderDetail(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer id = getInt(json, "orderId", 0);
                Double dlng = getDouble(json, "dlng", 0.0);
              Double dlat = getDouble(json, "dlat", 0.0);
                return orderService.getDeliveryOrderDetail(id,dlng,dlat);
            }
        });
    }

    /**
     * 已取货
     *
     * @param request
     * @param response
     */
    @RequestMapping("/pickupOrder")
    public void pickupOrder(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer id = getInt(json, "id", 0);
                Integer disId = getInt(json, "disId", 0);
                return orderService.pickupOrder(id, disId);
            }
        });
    }

    /**
     * 已送达
     *
     * @param request
     * @param response
     */
    @RequestMapping("/deliveredOrder")
    public void deliveredOrder(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer id = getInt(json, "id", 0);
                Integer disId = getInt(json, "disId", 0);
                return orderService.deliveredOrder(id, disId);
            }
        });
    }
    /**
     *
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getOrderByIdAndTime")
    public void getOrderByIdAndTime(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                int orderState = getInt(json, "orderState", 6);
                int disId = getInt(json, "disId", 0);
                int nowPage = getInt(json, "nowPage", 1);
                int pageSize = getInt(json, "pageSize", 20);
                String starTime = getString(json, "startTime", "");
                String endTime = getString(json, "endTime", "");
                Double dlng = getDouble(json, "dlng", 117.14917829518997000000);
                Double dlat = getDouble(json, "dlat", 36.66692040377557000000);
                return orderService.getOrderByIdAndTime(orderState, disId,nowPage,pageSize,starTime,endTime,dlng,dlat);
            }
        });
    }
    /**
     * 修改催单为知道了
     *
     */
    @RequestMapping("/updateReminderState")
    public void updateReminderState(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String orderId = getString(json, "orderId", "");
                int disId = getInt(json, "disId", 0);

                return orderService.updateReminderState(orderId, disId);
            }
        });
    }
    /**
     *
     * 功能描述:
     *
     * @param: 商家同意退款之后，商家点击确认归还货物之后配送员确认归还货物
     * @return:
     * @auther: wsl
     * @date: 2018/11/22 10:41
     */
    @RequestMapping("/deliverySure")
    public void deliverySure(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer orderId = getInt(json, "orderId", 0);
                Integer companyIsSure = getInt(json, "companyIsSure", 2);
                return orderService.companySure(orderId,companyIsSure);
            }
        });
    }
}
